﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ADO.NET.Samples.Common;
using System.Xml.Linq;

namespace ADO.NET.Samples.Forms.LINQ
{

    [FormDescription("LINQ to XML", "Demonstrate how to use LINQ to query XML.")]
    public partial class LINQtoXML : Form
    {
        public LINQtoXML()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Bind data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LINQtoXML_Load(object sender, EventArgs e)
        {
            try
            {
                XDocument doc = new XDocument(new XElement("Books",
                    new XElement("Book", new XAttribute("ID", "1"), new XElement("Title", "Book 1")),
                            new XElement("Book", new XAttribute("ID", "2"), new XElement("Title", "Book 2")),
                            new XElement("Book", new XAttribute("ID", "3"), new XElement("Title", "Book 3"))
                            ));
                var books = from book in doc.Root.Elements("Book")
                            select new { ID = book.Attribute("ID").Value, Title = book.Element("Title").Value };
                foreach (var b in books)
                {
                    this.listBox1.Items.Add(b.Title);
                }
                
                MessageBox.Show(doc.ToString());
            }
            catch
                (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}
